package auth.ssoclient.controller;

import auth.ssoclient.util.SpringMvcUtils;
import cn.dark.auth.core.LoginCore;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dark
 * @date 2023-10-29
 */
@Slf4j
@RestController
public class SsoClientController {

    @GetMapping("/isLogin")
    public boolean isLogin() {
        String token = SpringMvcUtils.getToken();
        return LoginCore.isLogin(token);
    }

    @GetMapping("/loginByTicket")
    public String loginByTicket(@RequestParam("ticket") String ticket) {
        String str = HttpUtil.get("http://127.0.0.1:8888/getLoginIdByTicket?ticket=" + ticket);
        JSONObject jsonObject = JSON.parseObject(str, JSONObject.class);
        log.info("根据ticket查询loginId，ticket:{}, result: {}", ticket, jsonObject);

        Object loginId = jsonObject.get("data");
        return LoginCore.login(Long.valueOf(loginId.toString()));
    }
}
